﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class BAKAB45AV95Data : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public INTEXDataSet setUI;
        string ABPOS = "";
        public BAKAB45AV95Data()
            : base()
        {

        }
        public BAKAB45AV95Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public BAKAB45AV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        public BAKAB45AV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy,INTEXDataSet _setUI)
            : base(_OracleDB, _proxy)
        {
            this.setUI = _setUI;
        }
        ~BAKAB45AV95Data()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
                string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();

                or1.CommandText = "SELECT ABPAUFKRS,ABPAUFLFD,ABPAUFPOS,ABPART, ABPVAR, ABPFARBE,ABPSOLLMENGE1 AS VWBEAMDIFF,ABPSOLLMENGE1 AS VWBEAMDIFPEC,"
                + "ABPSOLLMENGE1,ABPSOLLMENGE8,P.ABPWVDAT,ABPLIEFDAT,ABPSTARTDAT,ABPENDDAT,P.ABPERLKZ,P.ABPERLDAT,ABPNEUDAT"
                + " FROM ABPOS  P"
                + " WHERE ABPMANDANT='00' AND ABPERLKZ=:ABPERLKZ "
                + " AND ABPAUFKRS LIKE '%" + ABPAUFKRS
                + "%' AND ABPAUFLFD LIKE '%" + ABPAUFLFD
                + "%' AND ABPAUFPOS LIKE '%" + ABPAUFPOS
                + "%' AND ABPART LIKE '%" + ABPART + "%' "
                + " AND ABPLIEFNR LIKE '%" + ABPLIEFNR + "%' "
                + " ORDER BY  1,2,3";
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPSOLLMENGE8");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["ABPFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["ABPFARBE"] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet LoadABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
                string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT A.ABPAUFKRS,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR,A.ABPFARBE, "
                    + " A.ABPAUFLFD AS VWORDERNUM,A.ABPART AS ARTICLE,A.ABPSOLLMENGE1, "
                    + " A.ABPSOLLMENGE8,A.ABPWVDAT,A.ABPLIEFDAT, A.ABPSTARTDAT,A.ABPERLKZ, "
                    + " A.ABPERLDAT, A.ABPLIEFNR, C.LSNAME1 AS SPRODUCER,D.BSFBEZ,0 AS JSACTDATE "
                    + " FROM ABPOS A,ABFOLGE B,LIEFDAT C,BSFARB D "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT "
                    + " AND A.ABPAUFKRS = B.ABFAUFKRS AND A.ABPAUFLFD = B.ABFAUFLFD "
                    + " AND A.ABPAUFPOS = B.ABFAUFPOS AND B.ABFMANDANT = C.LSMANDANT "
                    + " AND B.ABFLIEFNR = C.LSLIEFNR AND A.ABPMANDANT = D.BSFMANDANT "
                    + " AND A.ABPART = D.BSFKART AND TRIM(ABPVAR) = D.BSFKVAR AND A.ABPFARBE = D.BSFKFARBE "
                    + " AND B.ABFMANUELKZ = 'N' AND TRIM(B.ABFAGANG) = 'SZ' "
                    + " AND A.ABPMANDANT = '00'AND A.ABPAUFART = 'VW' "
                    + " AND A.ABPAUFKRS LIKE '%" + ABPAUFKRS + "%' AND A.ABPAUFLFD LIKE '%" + ABPAUFLFD
                    + "%' AND A.ABPAUFPOS LIKE '%" + ABPAUFPOS + "%' AND A.ABPART LIKE '%" + ABPART + "%' "
                    + " AND C.LSLIEFNR LIKE '%" + ABPLIEFNR + "%' AND A.ABPERLKZ = :ABPERLKZ "
                //+ " AND ABFAUFLFD='000470'"
                    + " ORDER BY  SPRODUCER,ABPAUFKRS,ABPAUFLFD,ABPAUFPOS";
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPSOLLMENGE8");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column = "ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "SPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "BSFBEZ";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        dwTemp["VWORDERNUM"] = dwTemp["ABPAUFKRS"].ToString()
                           + "-" + dwTemp["ABPAUFLFD"].ToString() + "-" + dwTemp["ABPAUFPOS"].ToString();
                        dwTemp["ARTICLE"] = dwTemp["ABPART"].ToString().Trim()
                            + "-" + dwTemp["ABPFARBE"].ToString().Trim() + "-" + dwTemp["ABPVAR"].ToString().Trim();
                        GetABFOLGE(dwTemp);
                }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public override DataSet GetVKPOS(DataRow dwABPOS)
        {
            DataSet setLocal1 = base.GetVKPOS(dwABPOS);
            string ABAVKAUFTYP = "";
            string LSNAME1 = "";
            string sTemp = "";
            if (setLocal1.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow dwTemp in setLocal1.Tables["ABAUFM"].Rows)
                {
                    ABAVKAUFTYP = dwTemp["ABAVKAUFTYP"].ToString().Trim();
                    if (ABAVKAUFTYP == "K")
                    {
                        dwABPOS["VKPAUFKRS"] = dwTemp["ABAVKAUFKRS"];
                        dwABPOS["VKPAUFLFD"] = dwTemp["ABAVKAUFLFD"];
                        dwABPOS["VKPAUFPOS"] = dwTemp["ABAVKAUFPOS"];
                        dwABPOS["KDKUNDNA"] = dwTemp["KDKUNDNA"];
                        dwABPOS["ABARESQTY"] = dwTemp["ABARESQTY"];
                        dwABPOS["VKPMENGE1"] = dwTemp["VKPMENGE1"];
                        dwABPOS["STDBATCHNO"] = dwTemp["STDBATCHNO"];
                        dwABPOS["VKPWUTERM"] = dwTemp["VKPWUTERM"];
                        dwABPOS["VKPNEUDAT"] = dwTemp["VKPNEUDAT"];                        
                        dwABPOS["VKPAUFNUM"] = dwABPOS["VKPAUFKRS"].ToString()
                           + "-" + dwABPOS["VKPAUFLFD"].ToString() + "-" + dwABPOS["VKPAUFPOS"].ToString();
                    }
                }
            }
            return setLocal1;
        }
        private DataSet GetFFABRICEINFOByDI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFKRS as WBAUFKRS,P.ABPAUFLFD AS WBAUFLFD,P.ABPAUFPOS AS WBAUFPOS"
                    + ",LSLIEFNA,LSNAME1 AS FPRODUCER"
                + "  FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VA' AND P.ABPAUFKRS=M.ABAVKAUFKRS AND P.ABPAUFLFD=M.ABAVKAUFLFD AND P.ABPAUFPOS=M.ABAVKAUFPOS"
                + " LEFT JOIN LIEFDAT L ON  P.ABPLIEFNR=L.LSLIEFNR"
                    + " WHERE M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["WBAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["WBAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["WBAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    string sTemp = "", FPRODUCER = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["FPRODUCER"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["FPRODUCER"] = value;
                        sTemp = value + ";";
                        if (!FPRODUCER.Contains(sTemp)
                            && sTemp != ";")
                        {
                            FPRODUCER += sTemp;
                        }
                    }
                    FPRODUCER += "***";
                    FPRODUCER = FPRODUCER.Replace(";***", "");
                    FPRODUCER = FPRODUCER.Replace("***", "");
                    FPRODUCER = FPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["FPRODUCER"] = FPRODUCER;
                    Tracer.Verbose(ABPOS + " 后整理厂", FPRODUCER);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetGFABRICEINFOByDI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT ABPART,ABPVAR,ABPFARBE,P.ABPAUFKRS as WBAUFKRS, "
                    + " P.ABPAUFLFD AS WBAUFLFD,P.ABPAUFPOS AS WBAUFPOS,P.ABPSOLLMENGE1 AS GMETER, " 
                    + " LSLIEFNA,LSNAME1 AS GPRODUCER  FROM ABAUFM M,ABPOS P,LIEFDAT L,ABFOLGE F "
                    + " WHERE M.ABAMANDANT = P.ABPMANDANT AND M.ABAVKAUFKRS = P.ABPAUFKRS "
                    + " AND M.ABAVKAUFLFD = P.ABPAUFLFD AND M.ABAVKAUFPOS = P.ABPAUFPOS "
                    + " AND P.ABPMANDANT = F.ABFMANDANT AND P.ABPAUFKRS = F.ABFAUFKRS AND P.ABPAUFLFD = F.ABFAUFLFD "
                    + " AND P.ABPAUFPOS = F.ABFAUFPOS AND F.ABFMANDANT = L.LSMANDANT "
                    + " AND F.ABFLIEFNR = L.LSLIEFNR AND P.ABPAUFART in ('DI','PR') "
                    + " AND M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GMETER");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string sTemp = "", GPRODUCER = "",  column;
                    double GMETER=0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        column = "ABPFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        column = "GPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[column] = value;
                        sTemp = value + ";";
                        if (!GPRODUCER.Contains(sTemp)
                           && sTemp != ";")
                        {
                            GPRODUCER += sTemp;
                        }
                        
                        GMETER += System.Convert.ToDouble(dwTemp["GMETER"].ToString().Trim());                      
                        dwABPOS["WBAUFKRS"] = dwTemp["WBAUFKRS"];
                        dwABPOS["WBAUFLFD"] = dwTemp["WBAUFLFD"];
                        dwABPOS["WBAUFPOS"] = dwTemp["WBAUFPOS"];
                        dwABPOS["ARTICLE"] = dwTemp["ABPART"].ToString().Trim()
                           + "-" + dwTemp["ABPFARBE"].ToString().Trim() + "-" + dwTemp["ABPVAR"].ToString().Trim();
                    }
                    GPRODUCER += "***";
                    GPRODUCER = GPRODUCER.Replace(";***", "");
                    GPRODUCER = GPRODUCER.Replace("***", "");
                    GPRODUCER = GPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["GPRODUCER"] = GPRODUCER; 
                    dwABPOS["GMETER"] = GMETER;
                    Tracer.Verbose(ABPOS + " 织造厂：", GPRODUCER);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public override DataSet GetReservedOrder(DataRow dwABPOS, ref ArrayList listDIOrder, ref ArrayList listPROrder,string OrderNum="")
        {
            BaseFunction m_Data = new AB50AV95Data();
            m_Data.proxy = this.proxy;
            m_Data.OracleDB = this.OracleDB;
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            try
            {
                dwNABPOS["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
                dwNABPOS["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
                dwNABPOS["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
                setResult = base.GetReservedOrder(dwNABPOS, ref listDIOrder, ref listPROrder, OrderNum);
                if (setResult.Tables.Count > 0)
                {
                    byte[] b;
                    string value;
                    string OrderType = "";
                    string sTemp = "";
                    string SIZEBATCHNO = "";
                    string SPRODUCER = "";
                    foreach (DataRow dwTemp in setResult.Tables["RESERVEDORDER"].Rows)
                    {
                        OrderType = dwTemp["ABPAUFART"].ToString().Trim();
                        if (OrderType != "GP")
                            continue;
                        dwABPOS["JSPENDDATE"] = dwTemp["ABPLIEFDAT"];
                        dwABPOS["JSAENDDATE"] = dwTemp["ABPERLDAT"];            
                    } 
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return setReturn;
        }
        public DataSet GetMATYARNINFO(DataRow dwUIABPOS, DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            string ZUAUFNUM = "";
            try
            {
                /*
                  SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7 FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS
WHERE GAAUFKRS='PR' AND  GAAUFLFD='100428' AND GAAUFPOS='010'
                 */
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT GAAUFKRS||GAAUFLFD||GAAUFPOS AS ZUAUFNUM,GAAUFKRS,GAAUFLFD,GAAUFPOS"
                +",GAPOS,'' as ZUNEUDAT, '' AS GAYARN,ROUND(0.0001*GSGARNNR,1) AS GSGARNNR,GASPUL,GAART,GAVAR,GAFARBE,GSCBEZ,ZUSPPART,LSNAME1 AS GASUPPLIER,"
                +" GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7"
                +" FROM ABPOS P JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS"
                +" LEFT JOIN GSCOLOR F ON GSCART=GAART AND GSCVAR=GAVAR AND GSCFARBE=GAFARBE "
                +" LEFT JOIN GSDAT ON GSART=GAART AND GSVAR=GAVAR "
                +" LEFT JOIN ZUTEI Z ON ZUAUFKRS=GAAUFKRS AND ZUAUFLFD=GAAUFLFD  AND ZUAUFPOS=GAAUFPOS AND ZUPOS=GAPOS "
                +" LEFT JOIN GPDAT ON  ZUPARTIE=GPPARTIE"
                +" LEFT JOIN LIEFDAT L  ON   GPLIEFNR=L.LSLIEFNR"               
                + " WHERE ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD AND ABPAUFPOS=:ABPAUFPOS"
                +" ORDER BY 1,2,3,4,5";

                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                int weftcount = 0;
                string sTemp = "", GWARPYARNINFO = "";
                decimal GAKG1 = 0, GAKG3 = 0, GAKG5 = 0, GAKG6 = 0;

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    
                    tableLocal.TableName = "GAAUF";
                    SetDefault(tableLocal);
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG3");
                    Q100.Enqueue("GAKG5");
                    Q100.Enqueue("GAKG6");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string colum = "ZUSPPART";
                    double ZUNEUDAT = 0, JSACTDATE = 0;
                    JSACTDATE = System.Convert.ToDouble(dwABPOS["JSACTDATE"].ToString());
             
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        colum = "ZUSPPART";
                        #region  "ZUSPPART";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[colum].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[colum] = value;
                        #endregion
                        colum = "GAFARBE";
                        #region  "GAFARBE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[colum].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[colum] = value;
                        #endregion
                        colum = "GSCBEZ";
                        #region  "GSCBEZ";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[colum].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[colum] = value;
                        #endregion                     
                        colum = "GASUPPLIER";
                        #region  "GASUPPLIER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[colum].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[colum] = value;
                        #endregion                     
                        
                        dwTemp["GAYARN"] = dwTemp["GAART"].ToString() + "." + dwTemp["GAVAR"].ToString() + "." + dwTemp["GAFARBE"].ToString();
                        ZUAUFNUM = dwTemp["GAAUFKRS"].ToString()
                   + "-" + dwTemp["GAAUFLFD"].ToString() + "-" + dwTemp["GAAUFPOS"].ToString();
                        dwTemp["ZUAUFNUM"] = ZUAUFNUM;
                        #region JSACTDATE
                        ZUNEUDAT = System.Convert.ToDouble(dwTemp["ZUNEUDAT"].ToString());
                        if (JSACTDATE < ZUNEUDAT)
                        {
                            JSACTDATE = ZUNEUDAT;
                        }
                        #endregion                       
          
                        #region GAAUF
                        weftcount++;
                        sTemp = dwTemp["GAKG1"].ToString().Trim();
                        GAKG1 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG3"].ToString().Trim();
                        GAKG3 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG5"].ToString().Trim();
                        GAKG5 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG6"].ToString().Trim();
                        GAKG6 = System.Convert.ToDecimal(sTemp);
                        if (GAKG1 > GAKG5)
                        {
                            sTemp = "";
                            sTemp += dwTemp["GAART"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAVAR"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAFARBE"].ToString().Trim();
                            sTemp += "总需求" + GAKG1.ToString() + "kg,";
                            sTemp += "已留位" + GAKG3.ToString() + "kg,";
                            sTemp += "已发纱" + GAKG5.ToString() + "kg,";
                            sTemp += "已消耗" + GAKG6.ToString() + "kg.";
                            if (GAKG1 - GAKG3 - GAKG5 > 0)
                            {
                                GWARPYARNINFO += sTemp + "待料@" + (GAKG1 - GAKG3 - GAKG5).ToString() + "kg;";
                            }
                            else
                            {
                                GWARPYARNINFO += sTemp + ";";
                            }
                        }
                        #endregion
                    }                      
                    #region Finalize
                    GWARPYARNINFO += "***";
                    GWARPYARNINFO = GWARPYARNINFO.Replace(";***", "");
                    GWARPYARNINFO = GWARPYARNINFO.Replace("***", "");
                    //GWARPYARNINFO = GWARPYARNINFO.Replace(";", Environment.NewLine);
                    if (GWARPYARNINFO.Contains("待料"))
                    {
                        GWARPYARNINFO = "共" + weftcount.ToString() + "色经纱，其中" + GWARPYARNINFO;
                    }
                    else
                    {
                        GWARPYARNINFO = "原料OK。共" + weftcount.ToString() + "色经纱都已到位。";
                    }
                    dwABPOS["GWARPYARNINFO"] = GWARPYARNINFO;                    
                    dwABPOS["JSACTDATE"] = JSACTDATE;
                    if (JSACTDATE == 0)
                    {
                        dwABPOS["ABPLIEFDAT"] = 0;
                    }
                    #endregion
                    Tracer.Verbose(" 经纱信息", GWARPYARNINFO);
                    set4Return.Merge(setLocal);
                    if (setUI != null)
                    {
                        if (setUI.Tables.Contains("GAAUF"))
                        {
                            var query = from dwGAAUF in setUI.Tables["GAAUF"].AsEnumerable()
                                        where dwGAAUF["ZUAUFNUM"].ToString() == ZUAUFNUM
                                        select dwGAAUF;
                            if (query.Count() == 0)
                            {
                                setUI.Merge(setLocal);
                            }

                        }
                        else
                        {
                            setUI.Merge(setLocal);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetABFOLGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference
                /*
                  SELECT DISTINCT LSLIEFNA,LSNAME1,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS GLOOMNUM FROM ABFOLGE F,LIEFDAT L  WHERE ABFMANDANT='00'
AND   F.ABFAUFKRS='PR' AND  ABFAUFLFD='100428' AND  ABFAUFPOS='010'  AND F.ABFLIEFNR=L.LSLIEFNR
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT ABFRUECKDAT"
                + " FROM ABFOLGE "
                + " WHERE ABFMANDANT='00'  AND  ABFAUFKRS=:ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS "
                + " AND ABFAFOLGE = '010' ";

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    string sTemp = "", SPRODUCER = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        dwABPOS["JSACTDATE"] =  System.Convert.ToDouble(dwTemp["ABFRUECKDAT"].ToString());
                       /* b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["LSNAME1"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["LSNAME1"] = value;
                        sTemp = value + ";";
                        if (!SPRODUCER.Contains(sTemp)
                             && sTemp != ";")
                        {
                            SPRODUCER += sTemp;
                        }*/
                    }
                    /*SPRODUCER += "***";
                    SPRODUCER = SPRODUCER.Replace(";***", "");
                    SPRODUCER = SPRODUCER.Replace("***", "");
                    SPRODUCER = SPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["SPRODUCER"] = SPRODUCER;
                    Tracer.Verbose(ABPOS + " 浆纱厂", SPRODUCER);*/
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetBABST(DataRow dwUIABPOS,DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setResult= new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
               or1.CommandText = "SELECT DISTINCT BMBEM,BMSOLLLG, BMSOLLVLG,BMBAUMKRS,BMBAUMLFD FROM BABST M,BABEW W "
                   + " WHERE BMAUFKRS=BBAUFKRS AND BMAUFLFD=BBAUFLFD AND BMAUFPOS=BBAUFPOS "
                   + " AND BMAUFKRS=:BMARFKRS AND BMAUFLFD=:BMAUFLFD AND BMAUFPOS=:BMAUFPOS";
               or1.Parameters.Add("BMARFKRS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFKRS"].ToString();
               or1.Parameters.Add("BMAUFLFD", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFLFD"].ToString();
               or1.Parameters.Add("BMAUFPOS", OracleType.NVarChar).Value = dwUIABPOS["ABPAUFPOS"].ToString();
               setResult = Connect2Proxy(or1);

               if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "BABST";
                    Queue Q10 = new Queue();
                    Q10.Enqueue("BMSOLLLG");
                    Q10.Enqueue("BMSOLLVLG");
                    DivideBy10(tableLocal, Q10);
                    byte[] b;
                    string value;
                    string sTemp = "", SPRODUCER = "";
                   double BEAMALENG = 0;
                                   int ABEAMNO=0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["BMBEM"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["BMBEM"] = value; 
                        dwABPOS["BMBEM"] = dwTemp["BMBEM"];
                        BEAMALENG += System.Convert.ToDouble(dwTemp["BMSOLLVLG"].ToString());
                        ABEAMNO++; 
                    }
                   double ABPSOLLMENGE1;
                   ABPSOLLMENGE1=System.Convert.ToDouble(dwABPOS["ABPSOLLMENGE1"].ToString());
                   if (dwABPOS.Table.Columns.Contains("VWBEAMDIFF"))
                   {
                       dwABPOS["VWBEAMDIFF"] = System.Math.Round(ABPSOLLMENGE1 - BEAMALENG, 1);
                       dwABPOS["VWBEAMDIFPEC"] = System.Math.Round(100 * (ABPSOLLMENGE1 - BEAMALENG) / ABPSOLLMENGE1, 2);
                   }
                   
                    dwABPOS["BEAMALENG"] = BEAMALENG;
                   dwABPOS["ABEAMNO"] = ABEAMNO;
                }
               set4Return.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        #endregion
       public override void BuildABPOS(DataRow dwABPOS)
        {
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
         try
            { 
                dwNABPOS.ItemArray = dwABPOS.ItemArray;
                ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
                ABPOS = ThreadCount.ToString() + ": " + ABPOS;

                string ABAVKAUFTYP = "";
                string LSNAME1 = "";
                string sTemp = "";

                #region----------------------- 查询客单----------------------------
                GetVKPOS(dwNABPOS); 
                #endregion 
                #region----------------------- 查询浆纱回轴------------------------
                GetBABST(dwNABPOS, dwNABPOS);
                #endregion
                #region----------------------- 查询织造厂----------------------------
                GetGFABRICEINFOByDI(dwNABPOS);
                #endregion

                #region----------------------- 查询后整理厂---------------------------- 
               //GetFFABRICEINFOByDI(dwNABPOS); 
                #endregion

                #region-----------------------经纱信息----------------------------
               GetMATYARNINFO(dwNABPOS, dwNABPOS); 
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            { 
                try
                {
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;
                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
            } 
        }
    }
}